package controllers.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Duty;
import rbac.models.Oplog;
import rbac.models.User;

import com.et.mvc.JsonView;
import com.et.mvc.View;

import controllers.ApplicationController;

public class DutyController extends ApplicationController {
	public void index() {

	}

	public View getItems() throws Exception {
		List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();

		List<Duty> dutys = Duty.findAll(Duty.class, "parentid=0", null, "sort");
		for (Duty duty : dutys) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("id", duty.getId());
			item.put("text", duty.getName());
			items.add(item);
		}

		List<Map<String, Object>> doing = new ArrayList<Map<String, Object>>();
		doing.addAll(items);
		while (!doing.isEmpty()) {
			List<Map<String, Object>> todo = new ArrayList<Map<String, Object>>();
			for (Map<String, Object> item : doing) {
				dutys = Duty.findAll(Duty.class, "parentid=?",
						new Object[] { item.get("id") }, "sort");
				if (dutys.isEmpty()) {
					continue;
				}
				List<Object> children = new ArrayList<Object>();
				for (Duty duty : dutys) {
					Map<String, Object> child = new HashMap<String, Object>();
					child.put("id", duty.getId());
					child.put("text", duty.getName());
					child.put("state", duty.getChildCount() > 0 ? "closed"
							: "open");
					children.add(child);

					todo.add(child);
				}
				item.put("children", children.toArray(new Object[children
						.size()]));
			}
			doing = todo;
		}

		return new JsonView(items);
	}

	public View save(Duty item) throws Exception {
		Integer sort = (Integer) Duty.maximum(Duty.class, "sort", null, null);
		if (sort == null) {
			sort = 0;
		}
		item.setSort(++sort);
		item.save();
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新建职务，内容为：" + new JsonView(item));									

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", true);
		result.put("action", "append");
		result.put("id", item.getId());
		result.put("text", item.getName());
		View view = new JsonView(result);
		view.setContentType("text/plain; charset=utf-8");
		return view;
	}

	public View update(int id, Map<String, Object> params) throws Exception {
		Duty item = Duty.find(Duty.class, id);
		JsonView olditem= new JsonView(item);
		item = Duty.updateModel(item, params);
		item.save();
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改职务，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", true);
		result.put("action", "update");
		result.put("id", item.getId());
		result.put("text", item.getName());
		View view = new JsonView(result);
		view.setContentType("text/plain; charset=utf-8");
		return view;
	}

	public View destroy(int id) throws Exception {
		Duty item = Duty.find(Duty.class, id);
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除职务，内容为：" + new JsonView(item));									

		item.destroy();
		return new JsonView("success:true");
	}

	public View dnd(Integer id, Integer targetId, String point)
			throws Exception {
		Duty item = Duty.find(Duty.class, id);
		if (point.equals("append")) {
			Integer sort = (Integer) Duty.maximum(Duty.class, "sort", null,
					null);
			if (sort == null) {
				sort = 0;
			}
			item.setSort(++sort);
			item.setParentId(targetId);
		} else {
			Duty target = Duty.find(Duty.class, targetId);
			item.setParentId(target.getParentid());
			if (point.equals("top")) {
				Duty
						.execute(
								"update departments set sort=sort+1 where parentid=? and sort>=?",
								new Object[] { target.getParentid(),
										target.getSort() });
				item.setSort(target.getSort());
			} else {
				Duty
						.execute(
								"update departments set sort=sort+1 where parentid=? and sort>?",
								new Object[] { target.getParentid(),
										target.getSort() });
				item.setSort(target.getSort() + 1);
			}
		}
		item.save();
		return new JsonView("success:true");
	}
}
